package com.monitoring.application.service;

import com.monitoring.application.dto.EnvironmentalDataDTO;
import com.monitoring.domain.entity.EnvironmentalData;
import com.monitoring.domain.service.EnvironmentalMonitoringService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EnvironmentalDataApplicationService {
    private final EnvironmentalMonitoringService monitoringService;
    
    public EnvironmentalDataApplicationService(EnvironmentalMonitoringService monitoringService) {
        this.monitoringService = monitoringService;
    }
    
    // 记录环境数据
    public EnvironmentalDataDTO recordEnvironmentalData(EnvironmentalDataDTO dataDTO) {
        EnvironmentalData data = monitoringService.recordEnvironmentalData(
                dataDTO.getSensorId(),
                dataDTO.getTemperatureValue(),
                dataDTO.getHumidityValue(),
                dataDTO.getEnergyValue(),
                dataDTO.getDataType()
        );
        return convertToDTO(data);
    }
    
    // 根据传感器ID获取环境数据
    public List<EnvironmentalDataDTO> getEnvironmentalDataBySensorId(Long sensorId) {
        return monitoringService.getEnvironmentalDataBySensorId(sensorId).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    // 根据位置获取环境数据
    public List<EnvironmentalDataDTO> getEnvironmentalDataByLocation(String location) {
        return monitoringService.getEnvironmentalDataByLocation(location).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    // 获取特定时间段的环境数据
    public List<EnvironmentalDataDTO> getEnvironmentalDataByTimeRange(LocalDateTime start, LocalDateTime end) {
        return monitoringService.getEnvironmentalDataByTimeRange(start, end).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    // 获取传感器最新的环境数据
    public List<EnvironmentalDataDTO> getLatestEnvironmentalData(Long sensorId, String dataType, int limit) {
        return monitoringService.getLatestEnvironmentalData(sensorId, dataType, limit).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    // 处理高温情况
    public void handleHighTemperature(Long sensorId, double temperature) {
        monitoringService.handleHighTemperature(sensorId, temperature);
    }
    
    // 计算能耗成本
    public double calculateEnergyCost(Long deviceId, double pricePerKwh) {
        return monitoringService.calculateEnergyCost(deviceId, pricePerKwh);
    }
    
    // 转换实体到DTO
    private EnvironmentalDataDTO convertToDTO(EnvironmentalData data) {
        EnvironmentalDataDTO dto = new EnvironmentalDataDTO();
        dto.setId(data.getId());
        dto.setSensorId(data.getSensor().getId());
        dto.setSensorName(data.getSensor().getName());
        dto.setLocation(data.getSensor().getLocation());
        dto.setTemperatureValue(data.getTemperatureValue());
        dto.setHumidityValue(data.getHumidityValue());
        dto.setEnergyValue(data.getEnergyValue());
        dto.setTimestamp(data.getTimestamp());
        dto.setDataType(data.getDataType());
        return dto;
    }
}